package com.hbt.orderfood.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hbt.orderfood.R;
import com.hbt.orderfood.activity.MySubmitOrderActivity;
import com.hbt.orderfood.db.DBAdapter;
import com.hbt.orderfood.entitys.Order;
import com.hbt.orderfood.utils.StringUtils;

import java.util.LinkedList;
import java.util.List;

public class MySubmitOrderAdapter extends RecyclerView.Adapter {
    private List<Order> orders = new LinkedList<>();

    public TextView getTotal_price_text_view() {
        return total_price_text_view;
    }

    public void setTotal_price_text_view(TextView total_price_text_view) {
        this.total_price_text_view = total_price_text_view;
    }

    private TextView total_price_text_view;
    public int getTotal_price() {
        return total_price;
    }

    public void setTotal_price(int total_price) {
        this.total_price = total_price;
    }

    private int total_price = 0;
    private Context context;
    public MySubmitOrderAdapter(List<Order> orders){
        this.orders = orders;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        this.context = parent.getContext();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout layout = new LinearLayout(parent.getContext());
        layout.setLayoutParams(params);
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_submit_order_item,layout,true);
        ViewHolder holder = new ViewHolder(view);
        //        System.out.println("onCreateViewHolder");
        return holder;
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder hold = (ViewHolder)holder;
        Order order = orders.get(position);
        order.setFood(new DBAdapter(context).getFoodByFoodID(String.valueOf(order.getFood_id())));
        hold.tv_name.setText(order.getFood().getName());
        hold.tv_price.setText("价格："+String.valueOf(order.getPrice())+"￥");
        hold.tv_num.setText("数量："+String.valueOf(order.getNum()));
        StringUtils.setImg(hold.img,order.getFood().getType());
    }

    @Override
    public int getItemCount() {
        return orders.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_name;
        TextView tv_price;
        TextView tv_num;
        ImageView img;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.name);
            tv_price= itemView.findViewById(R.id.price);
            tv_num= itemView.findViewById(R.id.num);
            img = itemView.findViewById(R.id.img);
        }
    }
}
